package com.zhiwei.controller;

import com.github.pagehelper.PageInfo;
import com.zhiwei.configuration.WebUtils;
import com.zhiwei.pojo.Order;
import com.zhiwei.service.OrderService;
import com.zhiwei.vo.OrderList;
import com.zhiwei.vo.PageResult;
import com.zhiwei.vo.SysResult;
import com.zhiwei.vo.OrderInfo;
import com.zhiwei.vo.OrderTab;
import com.zhiwei.vo.PageResult;
import com.zhiwei.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 1.查询订单详情
     * 2.拦截请求并分页
     * 3.模糊查询实现
     * @return 返回查询到的数据
     */
    @GetMapping("/getlist")
    public SysResult getOrderList(PageResult pageResult) {
        PageInfo<Object> objectPageInfo = WebUtils.startPage()//启动分页拦截器
                .doSelectPageInfo(() -> {//执行查询业务
                   orderService.getOrderList(pageResult);
                });
        return SysResult.success(objectPageInfo);
    }

    /**
     * 删除订单详情
     * @param id 根据id删除用户--改变订单表中状态
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public SysResult deleteOrder(@PathVariable Integer id){
        int i = orderService.deleteOrder(id);
        if (i == 1){
            return SysResult.success();
        }
        return SysResult.fail();
    }

    /**
     * 入库
     * 去掉当前订单且要减少当前数量的货物，
     * @param id 根据id删除订单--改变订单表中状态
     * @return
     */
    @DeleteMapping("/outBound/{id}")
    public SysResult outBoundOrder(@PathVariable Integer id){
        int i = orderService.outBoundOrder(id);
        if (i == 1){
            return SysResult.success();
        }
        return SysResult.fail();
    }

    /**
     * 出库
     * 去掉当前订单且要减少当前数量的货物，
     * @param id 根据id删除订单--改变订单表中状态
     * @return
     */
    @DeleteMapping("/outBoundIn/{id}")
    public SysResult outBoundOrderIn(@PathVariable Integer id){
        int i = orderService.outBoundOrderIn(id);
        if (i == 1){
            return SysResult.success();
        }
        return SysResult.fail();
    }

    /**
     * 1.查询成功提交的订单详情
     * 2.拦截请求并分页
     * 3.模糊查询实现
     * @return 返回查询到的数据
     */
    @GetMapping("/getOrderOut")
    public SysResult getOrderOutList(PageResult pageResult) {
        PageInfo<Object> objectPageInfo = WebUtils.startPage()//启动分页拦截器
                .doSelectPageInfo(() -> {//执行查询业务
                    orderService.getOrderOutList(pageResult);
                    Integer type = pageResult.getType();

                });
        return SysResult.success(objectPageInfo);
    }

    /**
     * 获取已删除订单表
     * @return list
     */

    @GetMapping("/getRemove")
    public SysResult getRemoveList(){
        PageInfo<Object> objectPageInfo = WebUtils.startPage()//启动分页拦截器
                .doSelectPageInfo(() -> {//执行查询业务
                    orderService.getRemoveList();
                });
        return SysResult.success(objectPageInfo);
    }




    @GetMapping("/thisMonth")
    public SysResult getThisMonth(){
        OrderTab[] orderTabs = orderService.getThisMonth();
        return SysResult.success(orderTabs);
    }
    @GetMapping("/billList")
    public SysResult getBillList(PageResult pageResult){
        PageResult orders = orderService.getBillList(pageResult);
        if (orders != null){
            return SysResult.success(orders);
        }
        return SysResult.fail();
    }
    @GetMapping("/myOrderList")
    public SysResult getMyOrderList(PageResult pageResult){
        PageResult orders = orderService.getMyOrderList(pageResult);
        if (orders != null){
            return SysResult.success(orders);
        }
        return SysResult.fail();
    }
    @GetMapping("/info")
    public SysResult getOrderInfo(){
        OrderInfo orderInfo = orderService.getOrderInfo();
        if (orderInfo != null){
            return SysResult.success(orderInfo);
        }
        return SysResult.fail();
    }
    @PostMapping("/add")
    public SysResult addNewOrder(@RequestBody Order order){
        int i = orderService.addNewOrder(order);
        if (i != 0){
            return SysResult.success(i);
        }
        return SysResult.fail();
    }
    @GetMapping("/updateStatus/{id}")
    public SysResult updateStatus(@PathVariable Integer id){
        int i = orderService.updateStatus(id);
        return SysResult.success(i);
    }
}
